package com.csthink.limiter.config;

import com.csthink.limiter.manager.GuavaLimiter;
import com.csthink.limiter.manager.LimiterManager;
import com.csthink.limiter.manager.RedisLimiter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import static com.csthink.limiter.constant.ConfigConstant.LIMIT_TYPE;

/**
 * @author <a href="mailto:security.2009@live.cn">Mars</a>
 * @since 2023-09-08
 */
@Configuration
public class LimiterAutoConfiguration {

    @Bean
    @ConditionalOnProperty(name = LIMIT_TYPE, havingValue = "guava")
    public LimiterManager guavaLimiter() {
        return new GuavaLimiter();
    }


    @Bean
    @ConditionalOnProperty(name = LIMIT_TYPE, havingValue = "redis")
    public LimiterManager redisLimiter(StringRedisTemplate stringRedisTemplate) {
        return new RedisLimiter(stringRedisTemplate);
    }

    @Bean
    public LimiterProperties limiterProperties() {
        return new LimiterProperties();
    }
}
